//
//  ZZCRefundController.m
//  ZZC-Finance
//
//  Created by mac on 2017/6/10.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCRefundController.h"
#import "ZZCRefundCell.h"
#import "ZZCRefund.h"
#import "ZZCForgetPasswordOneController.h"

static NSString *refundCell = @"refundCell";
@interface ZZCRefundController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *refundTableView;
@property (strong, nonatomic) NSArray *refundData;
@property (nonatomic, strong) GRBkeyTextField * safeTextField;
@property (nonatomic, strong) GRBkeyTextField * safeInputTextField;
@property (nonatomic, strong) GRBsafeKeyBoard * board;

@property (nonatomic, assign) NSInteger repayID;
@property (nonatomic,   copy) NSString *payPwd;
@end

@implementation ZZCRefundController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"还款";
    [self requestDueData];
    [self.view addSubview:self.refundTableView];
//    [self bottomBtn];
    IQKeyboardManager *iq = [IQKeyboardManager sharedManager];
    iq.enableAutoToolbar = NO;

}


//- (void)bottomBtn {
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, ZZCScreenH-113, ZZCScreenW, 49)];
//    btn.backgroundColor = ZZCBGColor;
//    [btn setTitle:@"提前全额还款？" forState:0];
//    [btn addTarget:self action:@selector(extendCreditClick) forControlEvents:UIControlEventTouchUpInside];
//    btn.titleLabel.font = FONT(16);
//    [btn setTitleColor:[UIColor colorWithHex:0x75A1F6] forState:0];
//    [self.view addSubview:btn];
//}

////按钮监听
//- (void)extendCreditClick {
//    ZZCLog(@"你点击提前全额还款？");
//}
- (void)requestDueData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"credit_id":@(self.createID)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"token":token,@"credit_id":@(self.createID)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_CREDIT_CREDITLIST parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            self.refundData = [ZZCRefund mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            [self.refundTableView reloadData];
            
        }
    }];
}


#pragma mark  - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //填你后台返个数
    return _refundData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZZCRefundCell *cell = [tableView dequeueReusableCellWithIdentifier:refundCell];
    cell.selectionStyle = 0;
    cell.refundModel = self.refundData[indexPath.row];
    cell.refundBtn.tag = [cell.refundModel.id integerValue];
    [cell.refundBtn addTarget:self action:@selector(refundClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)refundClick:(UIButton *)sender {
    ZZCLog(@"你点了还款--%ld",(long)sender.tag);
    self.repayID = (long)sender.tag;
    [self alterTapClick];
}
#pragma mark - 监听点击事件
//点击还款支付密码
- (void)alterTapClick {
    
    self.board = [GRBsafeKeyBoard GRB_showSafeInputKeyBoard];
    __weak typeof(self)weakSelf = self;
    self.board.GRBsafeKeyFinish=^(NSString * passWord){
        
        if (passWord.length==6) {
            
            weakSelf.payPwd = passWord;
            [weakSelf.board GRB_endKeyBoard];
            ZZCLog(@"你输入的密码为---%@",passWord);
            [weakSelf clickRequestData];
            //在这里判断密码是否正确
            if (YES) {
                ZZCLog(@"支付密码正确");
                
            }else {
                ZZCLog(@"支付密码错误");
            }
        }
        
    };
    self.board.GRBsafeKeyClose=^{
        
        [weakSelf.board GRB_endKeyBoard];
    };
    
    self.board.GRBsafeKeyForgetPassWord=^{
        ZZCLog(@"点击忘记密码了");
        ZZCForgetPasswordOneController *forgetVC = [ZZCForgetPasswordOneController new];
        [weakSelf.navigationController pushViewController:forgetVC animated:YES];
    };
}
- (void)clickRequestData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"repay_id":@(_repayID),@"pay_password":_payPwd};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token,@"repay_id":@(_repayID),@"pay_password":_payPwd};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_CREDIT_REPAYMENT parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            [self requestDueData]; 
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 135;
}

- (UITableView *)refundTableView {
    if (_refundTableView == nil) {
        _refundTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ZZCScreenW, ZZCScreenH-113+49)];
        _refundTableView.backgroundColor = [UIColor colorWithHex:0xeaeaea];
        [_refundTableView registerNib:[UINib nibWithNibName:@"ZZCRefundCell" bundle:nil] forCellReuseIdentifier:refundCell];
        _refundTableView.delegate = self;
        _refundTableView.dataSource = self;
        _refundTableView.separatorStyle = NO;
        _refundTableView.showsVerticalScrollIndicator = NO;
    
    }
    return _refundTableView;
}


- (NSArray *)refundData {
    if (_refundData == nil) {
        _refundData = [NSArray array];
    }
    return _refundData;
}
@end
